package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.LoanOverdueStatistics;
import org.example.mapper.LoanOverdueStatisticsMapper;
import org.example.service.LoanOverdueStatisticsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 贷款逾期统计服务实现类
 * 
 * @author Generated
 */
@Slf4j
@Service
public class LoanOverdueStatisticsServiceImpl extends ServiceImpl<LoanOverdueStatisticsMapper, LoanOverdueStatistics> 
        implements LoanOverdueStatisticsService {
    
    @Override
    public List<LoanOverdueStatistics> queryByCondition(String loanType, String dimension) {
        LambdaQueryWrapper<LoanOverdueStatistics> queryWrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(loanType)) {
            queryWrapper.eq(LoanOverdueStatistics::getLoanType, loanType);
        }
        if (StringUtils.hasText(dimension)) {
            queryWrapper.eq(LoanOverdueStatistics::getDimension, dimension);
        }
        
        queryWrapper.orderByDesc(LoanOverdueStatistics::getCreatedAt);
        
        return list(queryWrapper);
    }
    
    @Override
    public void batchSave(List<LoanOverdueStatistics> dataList) {
        if (dataList != null && !dataList.isEmpty()) {
            saveBatch(dataList);
            log.info("批量保存贷款逾期统计数据，共 {} 条", dataList.size());
        }
    }
    
    @Override
    public List<LoanOverdueStatistics> getAllData() {
        LambdaQueryWrapper<LoanOverdueStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(LoanOverdueStatistics::getCreatedAt);
        return list(queryWrapper);
    }
    
    @Override
    public List<LoanOverdueStatistics> getByLoanType(String loanType) {
        LambdaQueryWrapper<LoanOverdueStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoanOverdueStatistics::getLoanType, loanType);
        queryWrapper.orderByDesc(LoanOverdueStatistics::getCreatedAt);
        return list(queryWrapper);
    }
    
    @Override
    public List<LoanOverdueStatistics> getByDimension(String dimension) {
        LambdaQueryWrapper<LoanOverdueStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoanOverdueStatistics::getDimension, dimension);
        queryWrapper.orderByDesc(LoanOverdueStatistics::getCreatedAt);
        return list(queryWrapper);
    }
    
    @Override
    public String importData(org.springframework.web.multipart.MultipartFile file) {
        // 这个方法在ExcelService中实现，这里保持空实现
        return "请使用Excel导入接口";
    }
    
    @Override
    public void exportData(HttpServletResponse response, String loanType, String dimension) {
        // 这个方法在ExcelService中实现，这里保持空实现
        try {
            response.getWriter().write("请使用Excel导出接口");
        } catch (IOException e) {
            log.error("写入响应失败", e);
        }
    }
    
    @Override
    public void exportTemplate(HttpServletResponse response) {
        // 这个方法在ExcelService中实现，这里保持空实现
        try {
            response.getWriter().write("请使用Excel模板导出接口");
        } catch (IOException e) {
            log.error("写入响应失败", e);
        }
    }
}
